package cn.yunyichina.provider.hquery.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.exception.CallException;
import cn.yunyichina.provider.hquery.dao.InspectDao;
import cn.yunyichina.provider.hquery.dao.InspectDetailDao;
import cn.yunyichina.provider.hquery.entity.Inspect;
import cn.yunyichina.provider.hquery.entity.InspectDetail;
import cn.yunyichina.provider.hquery.vo.ExaminationVo;
import cn.yunyichina.provider.iface.entity.base.MedicalCardVo;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.hisiface.examination.InspectDetailVoResponse;
import cn.yunyichina.provider.iface.entity.hisiface.examination.InspectListVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 * Created by 15-AK on 2016/12/15.
 */
public class InspectDetailRunnable  implements Runnable{
	
    private static final Logger logger = LoggerFactory.getLogger(InspectDetailRunnable.class);
    
    private List<InspectDetailVoResponse> inspectDetailVoResponseList;
    
    private ExaminationVo requestVo;
    
    private InspectDao inspectDao = SpringContextHandler.getBean(InspectDao.class);
    
    private InspectDetailDao inspectDetailDao = SpringContextHandler.getBean(InspectDetailDao.class);
    
    public InspectDetailRunnable(List<InspectDetailVoResponse> inspectDetailVoResponseList,ExaminationVo requestVo) {
        this.inspectDetailVoResponseList = inspectDetailVoResponseList;
        this.requestVo = requestVo;
    }

    @Override
    public void run() {

        logger.info("保存检验明细记录开始======inspectDetailVoResponseList ="+JsonUtils.toJsonString(inspectDetailVoResponseList));
        
        //存检验明细信息
        if (inspectDetailVoResponseList != null && inspectDetailVoResponseList.size() > 0) {
        	
        	//查找检验列表信息
        	Inspect inspect = new Inspect();
        	inspect.setOrgCode(requestVo.getHospitalCode());
        	inspect.setInspectId(requestVo.getInspectId());
			List<Inspect> inspectList = inspectDao.select(inspect);
			
			//需要插入的详情
			List<InspectDetail> insertInspectDetailList = null;
			if(inspectList != null && inspectList.size() == 1){
				
				//查找检验明细信息,不存在就插入
				InspectDetail inspectDetailPo = new InspectDetail();
				inspectDetailPo.setDataInspectId(inspectList.get(0).getId());
				List<InspectDetail> inspectDetailList = inspectDetailDao.select(inspectDetailPo);
				
				if(inspectDetailList.size() == 0){
					insertInspectDetailList = new ArrayList<InspectDetail>();
		    		InspectDetail inspectDetail = null;
		    		for(InspectDetailVoResponse inspectDetailVo : inspectDetailVoResponseList){
		    			inspectDetail = new InspectDetail();
		    			inspectDetail.setId(PKGenerator.generateId());
		    			inspectDetail.setDataInspectId(inspectList.get(0).getId());
		    			inspectDetail.setItemName(inspectDetailVo.getItemName());
		    			inspectDetail.setResult(inspectDetailVo.getResult());
		    			inspectDetail.setUnit(inspectDetailVo.getUnit());
		    			inspectDetail.setRefRange(inspectDetailVo.getRefRange());
		    			inspectDetail.setAbnormal(inspectDetailVo.getAbnormal());
		    			inspectDetail.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		    			
		    			insertInspectDetailList.add(inspectDetail);
		    		}
		    		
		    		if (CollectionUtils.isNotEmpty(insertInspectDetailList)) {
		        		//插入检查列表明细接口
		        		logger.info("要新增的检查明细=="+JsonUtils.toJsonString(insertInspectDetailList));
		        		inspectDetailDao.batchInsert(insertInspectDetailList);
		    		}
				}
			}
        }
    }
}
